﻿using Liju.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace LiJu.Db
{
     public class ProductImgDbHelper
    {
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <returns></returns>
        public static bool AddProductImg(ProductImgModel productImgModel)
        {
            string strMySql = $"INSERT INTO productimg (ProductId,WorkNum, ImgIndex, Path)VALUES('{productImgModel.ProductId}','{productImgModel.WorkNum}', '{productImgModel.ImgIndex}', '{productImgModel.Path}'); ";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        /// <summary>
        /// 修改产品图片
        /// </summary>
        /// <returns></returns>
        public static bool UpdateProductImg(ProductImgModel productImgModel)
        {
            string strMySql = $"update productimg set ProductId='{productImgModel.ProductId}',WorkNum= '{productImgModel.WorkNum}',ImgIndex= '{productImgModel.ImgIndex}', Path='{productImgModel.Path}' where Id={productImgModel.Id}";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }

        /// <summary>
        /// 查询所有产品
        /// </summary>
        /// <returns></returns>
        public static List<ProductImgModel> GetAllProductImgByProductId(long ProductId)
        {
            //获取没有删除的产品
            string strMySql = $"select * from productimg where ProductId={ProductId} and Status!=2  or Status is Null";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var products = DbExtension.DataTableToList<ProductImgModel>(dt);

            return products;
        }

        /// <summary>
        /// 查询所有产品
        /// </summary>
        /// <returns></returns>
        public static List<ProductImgModel> GetProductImgByProductIdGongXuNo(long ProductId,int GongXuNo)
        {
            //获取没有删除的产品
            string strMySql = $"select * from productimg where ProductId={ProductId} and WorkNum={GongXuNo} " +
                $"and (Status!=2  or Status is Null)";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            var products = DbExtension.DataTableToList<ProductImgModel>(dt);

            return products;
        }
        /// <summary>
        /// 删除产品
        /// 0:禁用 1:启用 2:删除
        /// </summary>
        public static bool DeleteProductImgByProductId(long ProductId)
        {
            string strMySql = $"update productimg  set Status=2 where ProductId={ProductId};";
            int result = DalMySQL.ExecuteNonQuery(strMySql);
            return result > 0;
        }
    }
}
